package com.csu.service.impl;

import com.csu.dao.UserDao;
import com.csu.dao.impl.UserDaoImpl;
import com.csu.entiry.User;
import com.csu.service.UserService;

/**
 * @author suhu
 * @AddedAuthor JIYI
 * @createDate 2021/12/2
 * @AddedCreateDate 2024/1/5
 */
public class UserServiceImpl implements UserService {

    private final UserDao userDao = new UserDaoImpl();

    public User login(String username, String password) {
        if (userDao.getByName(username) == null) {
            throw new RuntimeException("用户名不存在");
        }
        if (userDao.login(username, password)) {
            return userDao.getByName(username);
        } else {
            throw new RuntimeException("请输入正确的密码");
        }
    }

    public String Register(String username, String password, String repeatedPassword) {
        if (!repeatedPassword.equals(password)) {
            return "The password is different";
        }
        else if (userDao.isRepeatUserName(username) == 1){
            return "The username is exist";
        }
        if (username == null) {
            return "The username is empty";
        } else {
            int result = userDao.Register(username, password);
                User user = new User();
                user.setUsername(username);
                user.setRole("admin");
                return "success";
        }
    }
}
